﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using LuckyBingoRcon.Utilities;

namespace LuckyBingoRcon.Utilities
{
    public partial class InputBox : Form
    {
        public static int rVal = 0;
        private bool showDurationTextBox = false;
        static InputBox ib;
        static InputResult ir;

        public InputBox()
        {
        }

        public InputBox(string inputType, string playerName, Point location)
        {
            InitializeComponent();
            this.Location = location;
            this.textBoxSayMessage.Enabled = true;
            this.buttonSubmit.Enabled = false;
            this.textBoxTempBanDuration.Enabled = false;
            this.textBoxTempBanDuration.Visible = false;
            this.labelDuration.Visible = false;
            this.showDurationTextBox = false;
            switch (inputType)
            {
                case "Say To Player":
                    this.labelSayMessage.Text = "Enter a message for " + playerName + ":";
                    this.buttonSubmit.Text = "Say To Player";
                    break;
                case "Say To All":
                    this.labelSayMessage.Text = "Enter a message to the server:";
                    this.buttonSubmit.Text = "Say To All";
                    break;
                case "Kick Player":
                    this.labelSayMessage.Text = "Enter a reason for kicking " + playerName + ":";
                    this.buttonSubmit.Text = "Kick Player";
                    break;
                case "Ban Player":
                    this.labelSayMessage.Text = "Enter a reason for banning " + playerName + ":";
                    this.buttonSubmit.Text = "Ban Player";
                    break;
                case "Temp Ban Player":
                    this.labelSayMessage.Text = "Reason for temporarily banning " + playerName + ":";
                    this.labelDuration.Text = "Duration (minutes) for Temp Ban";
                    this.buttonSubmit.Text = "Temp Ban Player";
                    this.textBoxTempBanDuration.Enabled = true;
                    this.textBoxTempBanDuration.Visible = true;
                    this.labelDuration.Visible = true;
                    this.showDurationTextBox = true;
                    break;
                case "GameServers Login":
                    this.labelSayMessage.Text = "Enter the GameServers Login:";
                    this.labelDuration.Text = "Enter the GameServers Password:";
                    this.textBoxTempBanDuration.UseSystemPasswordChar = true;
                    this.buttonSubmit.Text = "Authenticate";
                    this.textBoxTempBanDuration.Enabled = true;
                    this.textBoxTempBanDuration.Visible = true;
                    this.labelDuration.Visible = true;
                    this.showDurationTextBox = true;
                    break;
                case "Server Name":
                    this.labelSayMessage.Text = "Enter a name for this server:";
                    if (!string.IsNullOrEmpty(playerName))
                        this.textBoxSayMessage.Text = playerName;
                    this.buttonSubmit.Text = "Save Name";
                    break;
            }            
        }

        public static InputResult ShowBox(string inputType, string playerName, Point location)
        {
            ib = new InputBox(inputType, playerName, location);
            ir = new InputResult();
            ib.ShowDialog();
            return ir;
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            ir.Message = this.textBoxSayMessage.Text;
            if (this.buttonSubmit.Text.Equals("Authenticate"))
                ir.Password = this.textBoxTempBanDuration.Text;
            else if (this.buttonSubmit.Text.Equals("Save Name"))
                ir.Password = null;
            else if (this.buttonSubmit.Text.Equals("Ban Player") || this.buttonSubmit.Text.Equals("Kick Player") || this.buttonSubmit.Text.Equals("Say To Player") || this.buttonSubmit.Text.Equals("Say To All") || this.buttonSubmit.Text.Equals("Temp Ban Player"))
                ir.Password = null;
            else
                ir.Duration = int.Parse(this.textBoxTempBanDuration.Text);
            ib.Dispose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            ir = null;
            ib.Dispose();
        }
        
        private void textBoxSayMessage_TextChanged(object sender, EventArgs e)
        {
            if (this.textBoxSayMessage.Text.Length > 0)
            {
                if (this.showDurationTextBox.Equals(false))
                {
                    this.buttonSubmit.Enabled = true;
                    this.AcceptButton = this.buttonSubmit;
                    return;
                }
                if (this.buttonSubmit.Text != "Authenticate")
                {
                    int dur = -1;
                    if (int.TryParse(this.textBoxTempBanDuration.Text, out dur) && dur >= 0)
                    {
                        this.buttonSubmit.Enabled = true;
                        this.AcceptButton = this.buttonSubmit;
                    }
                    else
                    {
                        this.buttonSubmit.Enabled = false;
                        this.AcceptButton = null;
                    }
                }
                else
                {
                    if (this.textBoxTempBanDuration.Text.Length > 0)
                    {
                        this.buttonSubmit.Enabled = true;
                        this.AcceptButton = this.buttonSubmit;
                    }
                    else
                    {
                        this.buttonSubmit.Enabled = false;
                        this.AcceptButton = null;
                    }
                }
            }
            else
            {
                this.buttonSubmit.Enabled = false;
                this.AcceptButton = null;
            }
        }

        private void textBoxTempBanDuration_TextChanged(object sender, EventArgs e)
        {
            if (this.buttonSubmit.Text != "Authenticate")
            {
                int dur = -1;
                if (int.TryParse(this.textBoxTempBanDuration.Text, out dur) && dur >= 0)
                {
                    if (this.textBoxSayMessage.Text.Length > 0)
                    {
                        this.buttonSubmit.Enabled = true;
                        this.AcceptButton = this.buttonSubmit;
                    }
                    else
                    {
                        this.buttonSubmit.Enabled = false;
                        this.AcceptButton = null;
                    }
                }
                else
                {
                    this.buttonSubmit.Enabled = false;
                    this.AcceptButton = null;
                }
            }
            else
            {
                if (this.textBoxTempBanDuration.Text.Length > 0)
                {
                    if (this.textBoxSayMessage.Text.Length > 0)
                    {
                        this.buttonSubmit.Enabled = true;
                        this.AcceptButton = this.buttonSubmit;
                    }
                    else
                    {
                        this.buttonSubmit.Enabled = false;
                        this.AcceptButton = null;
                    }
                }
                else
                {
                    this.buttonSubmit.Enabled = false;
                    this.AcceptButton = null;
                }
            }
        }
    }
}
